package org.test.entrasalida.excel;


import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public final class ReportPOIUtils {
	
	private ReportPOIUtils(){
		
	}

	private static final int FONT_8 = 8;
	private static final int FONT_10 = 10;
	private static final int FONT_12 = 12;
	private static HSSFFont arial12Bold;
	private static HSSFFont arial10Bold;
	private static HSSFFont arial8Bold;
	private static HSSFFont arial8;
	private static HSSFFont arial10;	
	private static HSSFCellStyle title12ReportFormat;
	private static HSSFCellStyle title10ReportFormat;
	private static HSSFCellStyle subTitle10BoldFormat;
	private static HSSFCellStyle headerTableFormat;
	private static HSSFCellStyle subHeaderTableFormat;
	private static HSSFCellStyle tableContentFormat;
	private static HSSFCellStyle tableContentDateFormat;	
	private static HSSFCellStyle tableContentYellowFormat;
	private static HSSFCellStyle tableContentGrayFormat;
	private static HSSFCellStyle tableContentGreenFormat;
	private static HSSFCellStyle tableContentOrangeFormat;
	private static HSSFCellStyle tableContentRedFormat;
	private static HSSFCellStyle tableContentGreenBoldFormat;
	private static HSSFCellStyle tableContentOrangeBoldFormat;
	private static HSSFCellStyle tableContentRedBoldFormat;
	private static HSSFCellStyle arial12BoldFormat;
	private static HSSFCellStyle arial10BoldBorderFormat;
	private static HSSFCellStyle arial10BoldBigBorderFormat;
	private static HSSFCellStyle defaultFormat;
	private static HSSFDataFormat dataFormat;
	public static final short REPORT_DEFAULT_SIZE = 3291;
	public static final short[] REPORT_1_SIZES = new short[]{2926,2926,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2560,2560,3657,2926,2560,6217,6217,2560,2560,2560,2560,2560,2560,2560,2560,2560,2560,2560,2560,6217};
	public static final short[] REPORT_2_SIZES = new short[]{2926,2926,2560,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,3657,2926,2560,2560,3291,3291,3291,3291,6217};
	public static final short[] REPORT_3_SIZES = new short[]{2926,2926,2560,2560,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2560,2560,3657,2926,2560,2560,6217,2560,6217,3291,3291,3291,3291,2926,2926,2926,2926,2926,2926,2926,2926,3291,3291,6217,3291,3291,3291};
	public static final short[] REPORT_4_SIZES = new short[]{2926,2926,2560,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2560,2560,3657,2926,2560,6217,2560,2560,2560,2560,2560,6217,3291,3291,6217,3291};
	public static final short[] REPORT_5_SIZES = new short[]{2926,2926,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2926,2926,2560,3291,3291,3291,6217,2560};
	public static final short[] REPORT_6_SIZES = new short[]{2926,2926,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2560,2560,3657,2926,2560,2560,2926,2926,2926,2926,2926,2926,2926,6217};
	public static final short[] REPORT_7_SIZES = new short[]{2926,2926,2560,2560,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2560,2560,3657,2926,2560,2560,6217,2560,6217,3291,3291,3291,3291,2926,2926,2926,2926,2926,2926,2926,2926,3291,3291,6217,3291,3291,3291,3291};
	public static final Map<Integer, short[]> MAP_SIZES_BY_REPORT = new HashMap<Integer, short[]>();

	public static HSSFCellStyle initCellStyle(HSSFWorkbook wb, HSSFFont font,
			Short align, Short verticalAlign, Short color, boolean wrapText,
			Short border) {
		
		HSSFCellStyle style = wb.createCellStyle();
		style.setFont(font);
		if (align != null) {
			style.setAlignment(align);
		}
		if (verticalAlign != null) {
			style.setVerticalAlignment(verticalAlign);
		}
		if (color != null) {
			style.setFillBackgroundColor(color);
			style.setFillForegroundColor(color);
			style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		}
		style.setWrapText(wrapText);
		if (border != null) {
			style.setBorderTop(border);
			style.setBorderBottom(border);
			style.setBorderLeft(border);
			style.setBorderRight(border);
		}
		return style;
	}

	private static void initFonts(HSSFWorkbook wb) {
		arial12Bold = wb.createFont();
		arial12Bold.setFontName(HSSFFont.FONT_ARIAL);
		arial12Bold.setFontHeightInPoints((short) FONT_12);
		arial12Bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		arial10Bold = wb.createFont();
		arial10Bold.setFontName(HSSFFont.FONT_ARIAL);
		arial10Bold.setFontHeightInPoints((short) FONT_10);
		arial10Bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		arial8Bold = wb.createFont();
		arial8Bold.setFontName(HSSFFont.FONT_ARIAL);
		arial8Bold.setFontHeightInPoints((short) FONT_8);
		arial8Bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		arial8 = wb.createFont();
		arial8.setFontName(HSSFFont.FONT_ARIAL);
		arial8.setFontHeightInPoints((short) FONT_8);
		
		arial10 = wb.createFont();
		arial10.setFontName(HSSFFont.FONT_ARIAL);
		arial10.setFontHeightInPoints((short) FONT_10);

	}

	public static void initFormat(HSSFWorkbook wb) {

		initFonts(wb);
		
		dataFormat = wb.createDataFormat();
		
		title12ReportFormat = initCellStyle(wb, arial12Bold,
				HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER,
				HSSFColor.LIGHT_YELLOW.index, true, null);

		title10ReportFormat = initCellStyle(wb, arial10Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER,
				HSSFColor.LIGHT_YELLOW.index, true, null);

		subTitle10BoldFormat = initCellStyle(wb, arial10Bold, null,
				HSSFCellStyle.VERTICAL_TOP, HSSFColor.WHITE.index, true, null);

		headerTableFormat = initCellStyle(wb, arial8Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER,
				HSSFColor.LIGHT_TURQUOISE.index, true,
				HSSFCellStyle.BORDER_THIN);

		subHeaderTableFormat = initCellStyle(wb, arial10Bold,
				HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER,
				HSSFColor.LIGHT_CORNFLOWER_BLUE.index, true,
				HSSFCellStyle.BORDER_THIN);

		tableContentFormat = initCellStyle(wb, arial8,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.WHITE.index, true, HSSFCellStyle.BORDER_THIN);
		
		//PR179AW-26: Use Date Format
		tableContentDateFormat = initCellStyle(wb, arial10,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.WHITE.index, true, HSSFCellStyle.BORDER_THIN);

		tableContentYellowFormat = initCellStyle(wb, arial10Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.YELLOW.index, true, HSSFCellStyle.BORDER_THIN);
		
		tableContentGrayFormat = initCellStyle(wb, arial8Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.GREY_25_PERCENT.index, true, HSSFCellStyle.BORDER_THIN);		
		
		tableContentGreenFormat = initCellStyle(wb, arial10Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.BRIGHT_GREEN.index, true, HSSFCellStyle.BORDER_THIN);

		tableContentOrangeFormat = initCellStyle(wb, arial8,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.LIGHT_ORANGE.index, true, HSSFCellStyle.BORDER_THIN);

		tableContentRedFormat = initCellStyle(wb, arial8,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.RED.index, true, HSSFCellStyle.BORDER_THIN);

		tableContentGreenBoldFormat = initCellStyle(wb, arial8Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.BRIGHT_GREEN.index, true, HSSFCellStyle.BORDER_THIN);

		tableContentOrangeBoldFormat = initCellStyle(wb, arial8Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.LIGHT_ORANGE.index, true, HSSFCellStyle.BORDER_THIN);

		tableContentRedBoldFormat = initCellStyle(wb, arial8Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_TOP,
				HSSFColor.RED.index, true, HSSFCellStyle.BORDER_THIN);

		arial12BoldFormat = initCellStyle(wb, arial12Bold, null, null,
				HSSFColor.WHITE.index, true, null);

		arial10BoldBorderFormat = initCellStyle(wb, arial10Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER,
				HSSFColor.WHITE.index, true, HSSFCellStyle.BORDER_THIN);

		arial10BoldBigBorderFormat = initCellStyle(wb, arial10Bold,
				HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER,
				HSSFColor.WHITE.index, true, HSSFCellStyle.BORDER_MEDIUM);
		
		defaultFormat = initCellStyle(wb, arial8, null, null, HSSFColor.WHITE.index, false, null);

	}

	public static HSSFFont getArial12Bold() {
		return arial12Bold;
	}

	public static HSSFFont getArial10Bold() {
		return arial10Bold;
	}

	public static HSSFFont getArial8Bold() {
		return arial8Bold;
	}

	public static HSSFFont getArial8() {
		return arial8;
	}

	public static HSSFCellStyle getTitle12ReportFormat() {
		return title12ReportFormat;
	}

	public static HSSFCellStyle getTitle10ReportFormat() {
		return title10ReportFormat;
	}

	public static HSSFCellStyle getSubTitle10BoldFormat() {
		return subTitle10BoldFormat;
	}

	public static HSSFCellStyle getHeaderTableFormat() {
		return headerTableFormat;
	}

	
	public static HSSFCellStyle getSubHeaderTableFormat() {
		return subHeaderTableFormat;
	}


	public static HSSFCellStyle getTableContentFormat() {
		return tableContentFormat;
	}

	public static HSSFCellStyle getTableContentDateFormat(String userDateFormat) {
		tableContentDateFormat.setDataFormat(dataFormat.getFormat(userDateFormat)); 
		return tableContentDateFormat;
	}	

	public static HSSFCellStyle getTableContentGreenFormat() {
		return tableContentGreenFormat;
	}

	public static HSSFCellStyle getTableContentOrangeFormat() {
		return tableContentOrangeFormat;
	}

	public static HSSFCellStyle getTableContentRedFormat() {
		return tableContentRedFormat;
	}

	public static HSSFCellStyle getTableContentGreenBoldFormat() {
		return tableContentGreenBoldFormat;
	}

	public static HSSFCellStyle getTableContentOrangeBoldFormat() {
		return tableContentOrangeBoldFormat;
	}

	public static HSSFCellStyle getTableContentRedBoldFormat() {
		return tableContentRedBoldFormat;
	}

	public static HSSFCellStyle getArial12BoldFormat() {
		return arial12BoldFormat;
	}

	public static HSSFCellStyle getArial10BoldBorderFormat() {
		return arial10BoldBorderFormat;
	}

	public static HSSFCellStyle getArial10BoldBigBorderFormat() {
		return arial10BoldBigBorderFormat;
	}

	public static HSSFCellStyle getDefaultFormat() {
		return defaultFormat;
	}

	public static HSSFCellStyle getTableContentGrayFormat() {
		return tableContentGrayFormat;
	}

	public static HSSFCellStyle getTableContentYellowFormat() {
		return tableContentYellowFormat;
	}

}
